<?php

namespace plugins\yongtai\controller;


use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use plugins\yongtai\controller\Base\BaseController;
use plugins\yongtai\model\ArticleCategoryModel;
use plugins\yongtai\model\ArticleModel;
use plugins\yongtai\model\SinglePageModel;
use plugins\yongtai\Utils;

class HomeController extends BaseController
{
    function Index()
    {
        $category_ids = (new ArticleCategoryModel())->where('pid', 20)->column('id');
        $category_ids[] = 20;
        $category_ids = implode(",", $category_ids);
        $news = (new ArticleModel())->whereIn("pid", $category_ids)->order('listorder desc,id desc')->select()->toArray();
        $this->assign('news', $news);
        return $this->Render(__CLASS__, __FUNCTION__, "home");
    }

    function News()
    {
        $pid = $this->request->get('pid', 20);
        $crumb = Utils::Crumb($pid, new ArticleCategoryModel());

        $this->assign('crumb', $crumb);
        $id = 20;
        if ($pid and $pid != 20) {
            $category = (new ArticleCategoryModel())->where('id', $pid)->find();
            $id = $category['pid'];
        }
        $nav = (new ArticleCategoryModel())->where(['pid' => $id])->order('listorder desc,id desc')->select();
        $this->assign('_nav', $nav);

        $category = (new ArticleCategoryModel())->where('id', $pid)->find();
        if ($category['pid'] == 0) {
            $category_ids = (new ArticleCategoryModel())->where('pid', $pid)->column('id');
            $category_ids[] = $pid;
            $category_ids = implode(",", $category_ids);
        } else {
            $category_ids = (new ArticleCategoryModel())->where('id', $pid)->column('id');
        }
        $data = (new ArticleModel())->whereIn("pid", $category_ids)->order('listorder desc,id desc')->select()->toArray();
        $this->assign('data', $data);
        return $this->fetch("home/home/news");
    }

    function NewsDetail()
    {
        $id = $this->request->get('id', 20);
        $data = (new ArticleModel())->where('id', $id)->find();
        $this->assign('data', $data);
        $crumb = Utils::Crumb($data['pid'], new ArticleCategoryModel());
        $this->assign('crumb', $crumb);
        $id = 20;
        if ($data['pid'] and $data['pid'] != 20) {
            $category = (new ArticleCategoryModel())->where('id', $data['pid'])->find();
            $id = $category['pid'];
        }
        $nav = (new ArticleCategoryModel())->where(['pid' => $id])->order('listorder desc,id desc')->select();
        $this->assign('_nav', $nav);

        return $this->fetch("home/home/news_detail");
    }

    function Single()
    {
        $id = $this->request->get('id', 20);
        $self = $this->request->get('self', false);
        $data = (new SinglePageModel())->where('id', $id)->find();
        $this->assign('data', $data);

        $nav = (new SinglePageModel())->where(['pid' => $data['pid']])->select();
        $current = $nav[0];
        $crumb = [
            ['title' => "首页", 'url' => "/"],
            ['title' => $current['title'], 'url' => cmf_plugin_url("yongtai://Home/Single", ['id' => $current['id']])],
        ];
        if ($current['id'] != $id) {
            $crumb[] = ['title' => $data['title'], 'url' => cmf_plugin_url("yongtai://Home/Single", ['id' => $data['id']])];
        }
        $this->assign('crumb', $crumb);
        if (!$self) unset($nav[0]);
        $this->assign('_nav', $nav);

        return $this->fetch("home/home/single");
    }

    function SendEmail()
    {
        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
        try {
            //服务器配置
            $mail->CharSet = "UTF-8";                     //设定邮件编码
            $mail->SMTPDebug = 0;                        // 调试模式输出
            $mail->isSMTP();                             // 使用SMTP
            $mail->Host = 'smtp.163.com';                // SMTP服务器
            $mail->SMTPAuth = true;                      // 允许 SMTP 认证
            $mail->Username = 'wdd13864743133@163.com';                // SMTP 用户名  即邮箱的用户名
            $mail->Password = 'SPTNFFCQCHBHEGXQ';             // SMTP 密码  部分邮箱是授权码(例如163邮箱)
            $mail->SMTPSecure = 'ssl';                    // 允许 TLS 或者ssl协议
            $mail->Port = 465;                            // 服务器端口 25 或者465 具体要看邮箱服务器支持

            $mail->setFrom('wdd13864743133@163.com', 'Mailer');  //发件人
            $mail->addAddress('783709770@qq.com', '-_-_-_-_-_');  // 收件人
            $mail->addAddress('suncx@taikesolution.com', 'suncx');  // 收件人
            //$mail->addAddress('ellen@example.com');  // 可添加多个收件人
//            $mail->addReplyTo('xxxx@163.com', 'info'); //回复的时候回复给哪个邮箱 建议和发件人一致
            //$mail->addCC('cc@example.com');                    //抄送
            //$mail->addBCC('bcc@example.com');                    //密送

            //发送附件
            // $mail->addAttachment('../xy.zip');         // 添加附件
            // $mail->addAttachment('../thumb-1.jpg', 'new.jpg');    // 发送附件并且重命名

            //Content
            $mail->isHTML(true);                                  // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容
            $mail->Subject = '这里是邮件标题' . time();
            $mail->Body = '<h1>这里是邮件内容</h1>' . date('Y-m-d H:i:s');
            $mail->AltBody = '如果邮件客户端不支持HTML则显示此内容';

            $mail->send();
            echo '邮件发送成功';
        } catch (Exception $e) {
            echo '邮件发送失败: ', $mail->ErrorInfo;
        }
    }
}